﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
public partial class Admin_ActivityTypeModify : System.Web.UI.Page
{
    //Page Load 
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    protected void Page_Load(object sender, EventArgs e)
    {
        //lblErrorMessage = (Label)Master.FindControl("lblErrorMessage");
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString.Count > 0) //add
                {
                    if (Convert.ToString(Request.QueryString["lgid"])=="0")
                    {
                        lblTitle.Text = "Add Email Server Master";
                        ClearData();
                    }
                    else //edit
                    {

                        lblTitle.Text = "Update Email Server Master";
                        GetData(Convert.ToInt64(Request.QueryString["lgid"]));
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Admin/EmailServerMaster.aspx");
            }
        }
    }

    //Clear Method
    private void ClearData()
    {
        
        txtActivityTypeName.Text = string.Empty;
        //txtActivityTypeCode.Text = string.Empty;
        TextBox1.Text = string.Empty;
        TextBox2.Text = string.Empty;
        TextBox3.Text = string.Empty;
        chkIsactive.Checked = false;
        lblMessage.Text = string.Empty;
    }

    //reterive data in update mode
    private void GetData(Int64 intLanguageID)
    {
        EmailServerMasterBAL objLMBal = new EmailServerMasterBAL();
        EmailServerMasterBO objLMBO = new EmailServerMasterBO();
        objLMBO = objLMBal.GetEmailMaster(intLanguageID);
        
        if (objLMBO != null)
        {
            txtActivityTypeName.Text = objLMBO.SmtpServerName;
            TextBox1.Text = objLMBO.SmtpServerEmail;
            TextBox2.Text = objLMBO.SmtpServerPassword;
            TextBox3.Text = objLMBO.SiteUrl;
            //txtActivityTypeCode.Text = objLMBO.ActivityCode;
            chkIsactive.Checked = Convert.ToBoolean(objLMBO.IsActive);
        }
    }

    //save button event
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //lblMessage.Text = "";
            if (Request.QueryString.Count > 0)
            {
               
                EmailServerMasterBAL objLMBal;
                EmailServerMasterBO objLMBO;
                //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
                int intLgid = Convert.ToInt16(Request.QueryString["lgid"]);
                string strmod = Convert.ToString(Request.QueryString["mod"]);
                string strReturnValue;

                if (strmod == "in")//add
                {
                    objLMBal = new EmailServerMasterBAL();
                    objLMBO = new EmailServerMasterBO();

                    objLMBO.SetMethod = "INSERT";
                    objLMBO.SmtpServerID = 0;
                    objLMBO.SmtpServerName = txtActivityTypeName.Text.Trim();
                    //objLMBO.ActivityCode = txtActivityTypeCode.Text.Trim();
                    objLMBO.SmtpServerEmail = TextBox1.Text;
                    objLMBO.SmtpServerPassword = TextBox2.Text;
                    objLMBO.SiteUrl = TextBox3.Text;
                    objLMBO.CreatedBy = gudUserID;
                    objLMBO.ModifyBy = gudUserID;
                    objLMBO.ModifyDate = System.DateTime.Today;
                    objLMBO.IsActive = Convert.ToBoolean(chkIsactive.Checked);
                    strReturnValue = (string)objLMBal.SetEmailMaster(objLMBO.SetMethod, objLMBO.SmtpServerID, objLMBO.SmtpServerName,objLMBO.SmtpServerEmail,objLMBO.SmtpServerPassword,objLMBO.SiteUrl ,objLMBO.CreatedBy, objLMBO.@ModifyBy, objLMBO.ModifyDate, objLMBO.IsActive);
                    if (strReturnValue == "SUCCESS")
                    {
                        ClearData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been save succesfully.');", true);
                        //lblMessage.Text = "Record has been save succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;

                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Email Server Name already exists.');", true);
                        //lblMessage.Text = "Email Server Name already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //  
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
                else if (strmod == "up")
                {

                    objLMBal = new EmailServerMasterBAL();
                    objLMBO = new EmailServerMasterBO();

                    objLMBO.SetMethod = "UPDATE";
                    objLMBO.SmtpServerID = intLgid;
                    objLMBO.SmtpServerName = txtActivityTypeName.Text.Trim();
                    //objLMBO.ActivityCode = txtActivityTypeCode.Text.Trim();
                    objLMBO.SmtpServerEmail = TextBox1.Text.Trim();
                    objLMBO.SmtpServerPassword = TextBox2.Text.Trim();
                    objLMBO.SiteUrl = TextBox3.Text.Trim();
                    objLMBO.ModifyBy = gudUserID;
                    objLMBO.ModifyDate = System.DateTime.Today;
                    objLMBO.IsActive = Convert.ToBoolean(chkIsactive.Checked);
                    strReturnValue = (string)objLMBal.SetEmailMaster(objLMBO.SetMethod, objLMBO.SmtpServerID, objLMBO.SmtpServerName,objLMBO.SmtpServerEmail,objLMBO.SmtpServerPassword,objLMBO.SiteUrl, objLMBO.CreatedBy, objLMBO.@ModifyBy, objLMBO.ModifyDate, objLMBO.IsActive);
                    if (strReturnValue == "SUCCESS")
                    {
                        ClearData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been updated succesfully.');", true);
                        //lblMessage.Text = "Record has been updated succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;

                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Activity Type already exists.');", true);
                        //lblMessage.Text = "Activity Type already exists."; //EnrollResources.GetMessage(Message.InsertMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }

            }
        }
    }

    //cancel button event
    protected void btnCancel_OnClick(object sender, EventArgs e)
    {
          if (Request.QueryString.Count > 0) //add
                {
                    if (Convert.ToString(Request.QueryString["lgid"]) == "0")
                    {
                        ClearData();
                        Response.Redirect("~/Admin/EmailServerMaster.aspx");
                    }
                    else
                    {
                        lblMessage.Text = string.Empty;
                        Response.Redirect("~/Admin/EmailServerMaster.aspx");
                        GetData(Convert.ToInt64(Request.QueryString["lgid"]));
                    }

            }
    }

  
}